﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using DAL;
using Common;


public partial class Stock_OutStock : System.Web.UI.Page
{
    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected int UserType;

    protected string DanNumClientID;

    protected void Page_PreRender(object sender, EventArgs e)
    {
            UserType = Convert.ToInt32(Session["UserType"]);
        
            OneDate.Attributes.Add("onclick","selectDate(this);");
            TwoDate.Attributes.Add("onclick", "selectDate(this);"); 

            //条件 Hashtable

            Hashtable tb = new Hashtable();
            bool QueryAdd = false;

            if (!string.IsNullOrEmpty(StName.Text.Trim()))
            {
                tb.Add("StName", StName.Text.Trim());
                QueryAdd = true;
            }
            if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
            {
                tb.Add("Keyword", Keyword.Text.Trim());
                QueryAdd = true;
            }
            if (OneDate.Text.Trim() != "选择开始日期")
            {
                tb.Add("OneDate", OneDate.Text.Trim());
                QueryAdd = true;
            }
            if (TwoDate.Text.Trim() != "选择结束日期")
            {
                tb.Add("TwoDate", TwoDate.Text.Trim());
                QueryAdd = true;
            }
            tb = QueryAdd ? tb : null;

            OutStockDataBind(tb);
    }

    private void OutStockDataBind(Hashtable QueryTable)
    {
        lv_OutStock.DataSource = new OutStock().SelectOutStock(QueryTable);
        lv_OutStock.DataBind();

        if (DataPager1.TotalRowCount == 0)
            DataPager1.Visible = false;
    }

    //搜索
    protected void submit_Click(object sender, EventArgs e)
    {
        Hashtable tb = new Hashtable();
        bool QueryAdd = false;

        //条件 Hashtable

        if (!string.IsNullOrEmpty(StName.Text.Trim()))
        {
            tb.Add("StName", StName.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
        {
            tb.Add("Keyword", Keyword.Text.Trim());
            QueryAdd = true;
        }
        if (OneDate.Text.Trim() != "选择开始日期")
        {
            tb.Add("OneDate", OneDate.Text.Trim());
            QueryAdd = true;
        }
        if (TwoDate.Text.Trim() != "选择结束日期")
        {
            tb.Add("TwoDate", TwoDate.Text.Trim());
            QueryAdd = true;
        }
        tb = QueryAdd ? tb : null;

        OutStockDataBind(tb);
    }


    protected void lv_OutStock_DataBound(object sender, EventArgs e)
    {
        DropDownList pagelist = (DropDownList)DataPager1.Controls[4].FindControl("PageList");

        int CurPage = WebUtility.GetCurrentPage(DataPager1.StartRowIndex, DataPager1.PageSize);
        int TotalPage = WebUtility.GetTotalPage(DataPager1.TotalRowCount, DataPager1.PageSize);
        for (int i = 1; i <= TotalPage; i++)
            pagelist.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
        pagelist.SelectedValue = CurPage.ToString();
    }

    //DropDownList分页选择
    protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList pagelist = (DropDownList)sender;

        int CurPage = Convert.ToInt32(pagelist.SelectedValue);
        int TotalPage = WebUtility.GetTotalPage(DataPager1.TotalRowCount, DataPager1.PageSize);

        DataPager1.SetPageProperties((CurPage - 1) * DataPager1.PageSize, DataPager1.PageSize, true);
    }

    //Cancel
    protected void lv_OutStock_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lv_OutStock.EditIndex = -1;
    }

    //Delete
    protected void lv_OutStock_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        new OutStock().DelOutStock((int)lv_OutStock.DataKeys[e.ItemIndex].Value);
    }

    //Edit
    protected void lv_OutStock_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lv_OutStock.EditIndex = e.NewEditIndex;

    }

    //Update
    protected void lv_OutStock_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        if (Convert.ToInt32(Session["UserType"]) == 4)   //管理员
        {
            TextBox txt_DanNum = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_DanNum");
            TextBox txt_Num = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_Num");
            TextBox txt_SalesCustomer = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_SalesCustomer");
            TextBox txt_SalesPrice = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_SalesPrice");
            TextBox txt_SalesAccountType = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_SalesAccountType");
            TextBox txt_OtherReason = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_OtherReason");

            decimal? de = null;

            Dat_OutStock outstock = new Dat_OutStock
            {
                OutStockKey = Convert.ToInt32(lv_OutStock.DataKeys[e.ItemIndex].Value),
                DanNum = txt_DanNum.Text.Trim(),
                Num = Convert.ToInt32(txt_Num.Text.Trim()),
                SalesCustomer = txt_SalesCustomer.Text.Trim() == "" ? "" : txt_SalesCustomer.Text.Trim(),
                SalesPrice = txt_SalesPrice.Text.Trim() == "" ? de : Convert.ToDecimal(txt_SalesPrice.Text.Trim()),
                SalesAccountType = txt_SalesAccountType.Text.Trim() == "" ? "" : txt_SalesAccountType.Text.Trim(),
                OtherReason = txt_OtherReason.Text.Trim() == "" ? "" : txt_OtherReason.Text.Trim()
            };


            new OutStock().EditOutStock(outstock);
        }
        if (Convert.ToInt32(Session["UserType"]) == 3)   //工程师
        {
            TextBox txt_DanNum = (TextBox)lv_OutStock.Items[e.ItemIndex].FindControl("txt_DanNum");

            Dat_OutStock outstock = new Dat_OutStock
            {
                OutStockKey = Convert.ToInt32(lv_OutStock.DataKeys[e.ItemIndex].Value),
                DanNum = txt_DanNum.Text.Trim()
            };

            new OutStock().EditOutStockNO(outstock);
        }

        Hashtable tb = new Hashtable();
        bool QueryAdd = false;

        //条件 Hashtable

        if (!string.IsNullOrEmpty(StName.Text.Trim()))
        {
            tb.Add("StName", StName.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
        {
            tb.Add("Keyword", Keyword.Text.Trim());
            QueryAdd = true;
        }
        if (OneDate.Text.Trim() != "选择开始日期")
        {
            tb.Add("OneDate", OneDate.Text.Trim());
            QueryAdd = true;
        }
        if (TwoDate.Text.Trim() != "选择结束日期")
        {
            tb.Add("TwoDate", TwoDate.Text.Trim());
            QueryAdd = true;
        }
        tb = QueryAdd ? tb : null;

        OutStockDataBind(tb);

        lv_OutStock.EditIndex = -1;
    }

    protected void lv_OutStock_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        //编辑模板绑定RadioButtonList
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            try
            {
                Label lb_OutType = (Label)e.Item.FindControl("OutType");
                RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("txt_OutType");
                rbl.SelectedValue = lb_OutType.Text;
                rbl.Enabled = false;

                if (UserType == 4)  //管理员
                {
                    Panel SaleType = (Panel)e.Item.FindControl("SaleType");
                    Panel OtherType = (Panel)e.Item.FindControl("OtherType");
                    if (lb_OutType.Text == "销售")
                    {
                        SaleType.Visible = true;
                        OtherType.Visible = false;
                    }
                    else
                    {
                        SaleType.Visible = false;
                        OtherType.Visible = true;
                    }
                }

                if (UserType == 3) //工程师
                {
                    Panel SaleType_Edit = (Panel)e.Item.FindControl("SaleType_Edit");
                    Panel OtherType_Edit = (Panel)e.Item.FindControl("OtherType_Edit");
                    if (lb_OutType.Text == "销售")
                    {
                        SaleType_Edit.Visible = true;
                        OtherType_Edit.Visible = false;
                    }
                    else
                    {
                        SaleType_Edit.Visible = false;
                        OtherType_Edit.Visible = true;
                    }
                }

                DanNumClientID = ((TextBox)e.Item.FindControl("txt_DanNum")).ClientID;
            }
            catch (Exception ex) { }
        }
    }
}
